/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dpy.appmvdpy.Models.Exporters;

import com.dpy.appmvdpy.Facades.ClientesFacade;
import com.dpy.appmvdpy.Models.Clientes;
import com.dpy.appmvdpy.Models.Produtos;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author ac_psoares
 */
@Component
@Scope(value="request")
public class ClientExporter extends Exporter<Clientes> {
    
    @Autowired
    public ClientesFacade facade;
    
    public ClientExporter()
    {
      
    }
    
    public ClientExporter(File file)
    {
      super(file);
    }
    
    @Override
    public File doExport(List<Clientes> items) {
        
        int i = 0;
        for(Clientes c: items)
        {
            try {
                bWriter.append(StringUtils.leftPad(c.getCodigo(),4,"0"));//codigo
                bWriter.append("##");
                bWriter.append("0");
                bWriter.append("##");
                bWriter.append(StringUtils.leftPad(c.getVendedor().getCodigoDPY(),3,"0"));
                bWriter.append("##");
                bWriter.append(StringUtils.rightPad(c.getNome(),40));
                bWriter.append("##");
                bWriter.append(StringUtils.rightPad(c.getNomeFantasia(),40));
                bWriter.append("##");
                bWriter.append(StringUtils.rightPad(c.getEndereco(),40));
                bWriter.append("##");
                bWriter.append("     ");
                bWriter.append("##");
                bWriter.append(StringUtils.rightPad(c.getBairro(),25));
                bWriter.append("##");
                bWriter.append(StringUtils.rightPad(StringUtils.remove(c.getCep(),'-'),8));
                bWriter.append("##");
                bWriter.append(StringUtils.rightPad(c.getCidade(),30));
                bWriter.append("##");
                bWriter.append(c.getEstado());
                bWriter.append("##");                   
                bWriter.append(StringUtils.rightPad(c.getInscricao(),15));
                bWriter.append("##");
                bWriter.append("J");
                bWriter.append("##");
                bWriter.append(StringUtils.remove(StringUtils.remove(StringUtils.remove(c.getCnpj(),'.'),'/'),'-'));
                bWriter.append("##");
                bWriter.append(StringUtils.rightPad(c.getTelefone(),12));
                bWriter.append("##");
                bWriter.append(StringUtils.rightPad(c.getFax(),12));
                bWriter.append("##");
                bWriter.append(StringUtils.rightPad(c.getContato(),25));
                bWriter.append("##");
                bWriter.append( StringUtils.leftPad(String.valueOf(c.getDataCadastro().getDate()),2,"0") + StringUtils.leftPad(String.valueOf(c.getDataCadastro().getMonth() + 1),2,"0") + StringUtils.leftPad(String.valueOf(c.getDataCadastro().getYear() % 100),2,"0"));
                bWriter.append("##");
                bWriter.append(StringUtils.rightPad(c.getEmail(),35));
                bWriter.newLine();
                c.setIntegrado(Boolean.TRUE);
                
                facade.edit(c);                
                
                i++;
            } catch (IOException ex) {
                Logger.getLogger(ClientExporter.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        try {
            bWriter.flush();
        } catch (IOException ex) {
            Logger.getLogger(ClientExporter.class.getName()).log(Level.SEVERE, null, ex);
        }
        return this.out;
    }
}
